//
//  CourseTable.m
//  TMS
//
//  Created by Dhanashree Bodas on 29/01/15.
//  Copyright (c) 2015 Dhanashree Bodas. All rights reserved.
//

#import "CourseTable.h"
#import "DBManager.h"
#import "Course.h"

@implementation CourseTable
-(int) addCourse:(NSString *)coursename andplatform:(NSString *)platform anddescription:(NSString *)description
{
    self.db=[[DBManager alloc]initWithDatabaseFilename:@"tmsdb.sql"];
    
    NSString *query=[NSString stringWithFormat:@"insert into Course(coursename,platform,description) values('%@','%@','%@')",coursename,platform,description];
    
    [self.db executeQuery:query];
    if (self.db.affectedRows != 0)
    {
        return 1;
    }
    return 0;
    
    
    
}


-(NSArray *) listCourse
{

    self.db=[[DBManager alloc]initWithDatabaseFilename:@"tmsdb.sql"];
    NSString *query=@"select * from Course";
    NSArray *courseData=[self.db loadDataFromDB:query];
    NSMutableArray *cdata = [[NSMutableArray alloc] init];
    
    for(int i=0;i<courseData.count;i++)
    {
        
        Course *c=[[Course alloc]init];
        id temp = [courseData objectAtIndex:i];
        c.cid = [[temp objectAtIndex:[self.db.arrColumnNames indexOfObject:@"cid"] ] intValue];
        c.cname=[temp objectAtIndex:[self.db.arrColumnNames indexOfObject:@"coursename"]];
        c.cdesc=[temp objectAtIndex:[self.db.arrColumnNames indexOfObject:@"description"]];
        c.platform=[temp objectAtIndex:[self.db.arrColumnNames indexOfObject:@"platform"]];
        [cdata addObject:c];
    }
    return cdata;
    
}


-(int) deleteCourse:(int)cid{
    
    self.db=[[DBManager alloc] initWithDatabaseFilename:@"tmsdb.sql"];
    
    NSString *query=[NSString stringWithFormat:@"delete from Course where cid='%d'",cid];
    
    [self.db executeQuery:query];
    
    if(self.db.affectedRows!=0)
        return 1;
    return 0;
    
}

//Trainee Module
-(NSMutableArray *) LoadCourseWithCourseID:(int)cid{
    
    // Form the query.
    
    NSString *query = [NSString stringWithFormat:@"select * from Course where cid=%d",cid];
    NSArray *arrCourse=[[NSArray alloc] init];
    
    self.db=[[DBManager alloc] initWithDatabaseFilename:@"tmsdb.sql"];
    arrCourse=[self.db loadDataFromDB:query];
    
    NSMutableArray *result=[[NSMutableArray alloc]init];
    NSUInteger totalRows=arrCourse.count;
    
    NSArray *tempdata=[[NSArray alloc] init];
    
    for(int i=0;i<totalRows;i++)
    {
        Course *course=[[Course alloc]init];
        tempdata=[arrCourse objectAtIndex:i];
        course.cid = [[tempdata objectAtIndex:[self.db.arrColumnNames indexOfObject:@"cid"] ] intValue];
        course.cname=[tempdata objectAtIndex:[self.db.arrColumnNames indexOfObject:@"coursename"]];
        course.platform=[tempdata objectAtIndex:[self.db.arrColumnNames indexOfObject:@"platform"]];
        course.cdesc=[tempdata objectAtIndex:[self.db.arrColumnNames indexOfObject:@"description"]];
        [result addObject:course];
        
    }
    
    return result;
    
}

@end
